import { ServiceAll } from '../../interface'
import { UserServiceImpl } from './user'
import { UserRoleServiceImpl } from './role'
import { UserPermissionServiceImpl } from './permission'
import { logger } from 'f2e-server3'
import { Db } from 'mongodb'

export interface ServiceCreatorProps {
    /**
     * mongodb数据链接
    */
    db: Db;
    /** 支持免登录的用户名,需要跟持久化数据的用户名一致 */
    mock_user?: string;
}
export const createService = (props: ServiceCreatorProps): ServiceAll  => {
    const { db, mock_user } = props
    const userService = new UserServiceImpl(db, mock_user)
    const userRoleService = new UserRoleServiceImpl(db)
    const userPermissionService = new UserPermissionServiceImpl(db)
    return {
        userService,
        userRoleService,
        userPermissionService,
        init: async function (data) {
            const perms = await userPermissionService.permissionList({})
            if (!perms.data?.total) {
                logger.info('初始化权限数据')
                await userService.userCreate(data.users)
                await userRoleService.roleCreate(data.roles)
                await userPermissionService.permissionCreate(data.permissions)
                logger.info('初始化权限数据完成')
            }
        },
    }
}
export default createService;
